package net.zjitc.dao.user.userDaoImpl;

import net.zjitc.dao.user.UserDao;
import net.zjitc.domain.User;
import net.zjitc.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * Created by hasee on 2017/10/20.
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User findByUsername(String username) {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        String sql = "select * from user where username = ?";
        User user = null;
        try {
            user = (User) runner.query(sql,new BeanHandler(User.class),username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public void insert(User newUser) {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        String username = newUser.getUsername();
        String password = newUser.getPassword();
        String phone = newUser.getPhone();
        String sql = "insert into user (username,password,phone) value(?,?,?)";
        try {
            runner.update(sql,username,password,phone);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
